﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AutoUpdateMgr.Mgr
{
    internal class PacketMgr
    {
        #region Instance

        public static PacketMgr instance { set; get; } = null;

        public static PacketMgr getInstance()
        {
            if (instance == null)
            {
                instance = new PacketMgr();
            }

            return instance;
        }

        #endregion


        public void ProcessPacket(string packetString)
        {
            try
            {
                AutoUpdate.Model.Packet.Packet packet = this.getPacket(packetString);

                //packet.clientSessionID = session.channelHashCode;

                HandlerMgr.getInstance().doAction(packet);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
        }
        public AutoUpdate.Model.Packet.Packet getPacket(string msg)
        {
            AutoUpdate.Model.Packet.Packet result = null;

            try
            {

                result = Newtonsoft.Json.JsonConvert.DeserializeObject<AutoUpdate.Model.Packet.Packet>(msg);

            }
            catch (Exception ex)
            {
                throw ex;
            }

            return result;
        }

        public string getPacketString(AutoUpdate.Model.Packet.Packet packet)
        {
            string result = "";

            result = Newtonsoft.Json.JsonConvert.SerializeObject(packet);

            return result;
        }
    }
}
